package cetcbigdata.da.legal_person_service.hainan;

import cetcbigdata.da.base.Base;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

public class LPSSpiderHN extends Base {
    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {

    }

    @Override
    public JSONObject getDataDemo() {
        return null;
    }

    public void getInfo() {
        //按地区分类

        //获取市、县一级列表
        String SXJUrl = "https://wssp.hainan.gov.cn/wssp3/address/exempt/getAddress?";
        JSONObject SXJHeader = new JSONObject();
        SXJHeader.put("Referer", "https://wssp.hainan.gov.cn/hnwt/perserver");
        SXJHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");

        String SXJStrResponse = getReq(SXJUrl,SXJHeader,null);
        JSONObject SXJResponse = JSONObject.parseObject(SXJStrResponse);
        for (Object o : SXJResponse.getJSONObject("data").getJSONArray("horizonDeptList")){
            JSONObject SXJReqData = (JSONObject) o;
            String SXJId = SXJReqData.getString("id");

            //获取区、镇一级列表
            String QZJUrl = "https://wssp.hainan.gov.cn/wssp3/address/exempt/getAddress?deptId=" + SXJId;
            JSONObject QZJHeader = new JSONObject();
            QZJHeader.put("Referer", "https://wssp.hainan.gov.cn/hnwt/perserver");
            QZJHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");

            String QZJStrResponse = getReq(QZJUrl,QZJHeader, null);
            JSONObject QZJResponse = JSONObject.parseObject(QZJStrResponse);
            JSONArray QZJDeptList = QZJResponse.getJSONObject("data").getJSONArray("horizonDeptList");
            String region;
            if (null != QZJDeptList && QZJDeptList.size() > 0) {
                for (Object o1 : QZJDeptList) {
                    JSONObject QZJReqData = (JSONObject) o1;
                    String QZJId = QZJReqData.getString("id");
                    region ="海南省-" + SXJReqData.getString("deptName") + "-" + QZJReqData.getString("deptName");
                    parseListPage(region, QZJId);
                }
            }else {
                region ="海南省-" + SXJReqData.getString("deptName");
                parseListPage(region, SXJId);
            }
        }
    }

    public JSONObject parseListPage(String region, String regionId){
        //创建获取详情页信息的对象
        LPSAllInfo allInfo = new LPSAllInfo();

        //按主题分类
        String themeUrl = "https://wssp.hainan.gov.cn/wssp3/matter/exempt/getThemeOfNumber";
        JSONObject themeHeader = new JSONObject();
        JSONObject themeParams = new JSONObject();
        themeHeader.put("Referer", "https://wssp.hainan.gov.cn/hnwt/business");
        themeHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
        themeParams.put("xzqhid", "HZ54552851f301050151f6cf2e000456");
        themeParams.put("themetype", "14");
        JSONObject themeResp = JSONObject.parseObject(postFormReq(themeUrl, themeHeader, themeParams));
        JSONArray themes = themeResp.getJSONArray("data");

        //循环主题
        for (Object obj : themes) {
            JSONObject theme = (JSONObject) obj;
            String module = "法人服务-" + theme.getString("sort_name");
            //======获取详情页列表=======
            //当前页
            int currentPage = 1;
            //总页数
            int pags = 0;
            while (pags != currentPage) {
                String listUrl = "https://wssp.hainan.gov.cn/wssp3/matter/exempt/info";
                JSONObject listHeader = new JSONObject();
                listHeader.put("Referer", "https://wssp.hainan.gov.cn/hnwt/perserver");
                listHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
                JSONObject listParmas = new JSONObject();
                listParmas.put("qrys", "{'serverType':'2'," +
                        "'serviceSubType':'theme'," +
                        "'theme':'"+theme.getString("sort_code")+"'," +
                        "'dept':''," +
                        "'regionId':'"+regionId+"'," +
                        "'taskType':''," +
                        "'isOnLine':''," +
                        "'taskName':''," +
                        "'pageNumber':"+currentPage+"," +
                        "'pageSize':10," +
                        "'mltaskname':''}");
                String listStrResponse = postFormReq(listUrl, listHeader, listParmas);
                JSONObject listResponse = JSONObject.parseObject(listStrResponse);
                for (Object o2 : listResponse.getJSONObject("data").getJSONArray("shixiangList")) {
                    JSONObject shixiang = (JSONObject) o2;

                    JSONObject result = new JSONObject();
                    JSONArray pageInfoList = new JSONArray();


                    for (Object o3 : shixiang.getJSONArray("subdatas")) {
                        JSONObject subdata = (JSONObject) o3;
                        String taskname = subdata.getString("taskname");
                        String id = subdata.getString("id");
                        String sourcekey = subdata.getString("sourcekey");
                        allInfo.setId(id);
                        allInfo.setSourcekey(sourcekey);
                        allInfo.init();
                        JSONObject pageInfo = allInfo.getAllInfo();
                        pageInfo.put("名称", taskname);
                        pageInfo.put("地址", "https://wssp.hainan.gov.cn/hnwt/handlingGuideline?id="+id+"&sourcekey="+sourcekey);
                        pageInfoList.add(pageInfo);
                    }
                    result.put("名称", shixiang.getString("taskname"));
                    result.put("选项", pageInfoList);
                    System.out.println(currentPage + "\n"
                            + region + "\n"
                            + module + "\n"
                            + result
                    );
//                    return processResult(
//                            null,
//                            null,
//                            null,
//                            null,
//                            result,
//                            region,
//                            module
//                    );
                }
                //更新当前、总页数
                currentPage++;
                pags = listResponse.getJSONObject("data").getJSONObject("page").getInteger("pageCount") + 1;
            }
        }
        return null;
    }

    public static void main(String[] sarg){
        LPSSpiderHN lpsSpiderHN = new LPSSpiderHN();
        lpsSpiderHN.getInfo();
    }
}
